package cn.edu.hzvtc.service.impl;

import cn.edu.hzvtc.dao.MsgAnswerMapper;
import cn.edu.hzvtc.dao.MsgMapper;

import cn.edu.hzvtc.pojo.Msg;
import cn.edu.hzvtc.pojo.MsgAnswer;
import cn.edu.hzvtc.pojo.MsgCount;
import cn.edu.hzvtc.service.msgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

@Service("msgService")
public class msgServiceImpl implements msgService {
    @Autowired
    private MsgMapper msgMapper;
    @Autowired
    private MsgAnswerMapper msgAnswerMapper;

    @Override
    public List<Msg> getSendMsgByUserId(Integer userId) {
        return msgMapper.selectSendMsgByUserId(userId);
    }


    @Override
    public Boolean deleteMsg(Integer deleteId) {
        Boolean result = false;
        if (msgMapper.selectSendMsgByMsgState(deleteId) != null) {
            if (msgMapper.delStateByPrimaryKey(deleteId) != 0) {
                result = true;
            }
            else {
                return false;
            }
        }
        return  result;
    }


    @Override
    public Integer addMsg(Msg msg, Set<Integer> receiveUserId) {
        if (msgMapper.insert(msg) != 0) {
            System.out.println("msgid"+msg.getId());
            List<MsgAnswer> msgAnswers = new ArrayList<MsgAnswer>();
            for (Integer i : receiveUserId) {
                MsgAnswer answer = new MsgAnswer();
                answer.setMsgId(msg.getId());
                answer.setMsgReceiveUserId(i);
                answer.setMsgState(0);
                msgAnswers.add(answer);
            }
            System.out.println("A");
            msgAnswerMapper.insertMsgAnswers(msgAnswers);
            return 1;
        }
        return 0;
    }

    @Override
    public Integer getCountByUserId(Integer userId,Integer msgState) {
        return msgAnswerMapper.selectCountByUserId(userId,msgState);
    }

    @Override
    public List<MsgAnswer> getReceiveMsgByUserId(Integer userId){
        return msgAnswerMapper.selectReceiveMsgByUserId(userId);
    }

    @Override
    public Integer updateMsgAnswer(MsgAnswer msgAnswer) {
        System.out.println(msgAnswer.getId());
        msgAnswer.setMsgState(1);
        msgAnswer.setMsgAnswerTime(new Date());
        msgAnswer.setMsgAnswerContent(msgAnswer.getMsgAnswerContent());
        msgAnswer.setMsgVote(msgAnswer.getMsgVote());
        msgAnswer.setMsgVoteReason(msgAnswer.getMsgVoteReason());
        return msgAnswerMapper.updateMsgAnswerById(msgAnswer);
    }




    @Override
    public Msg getMsgById(Integer id) {
        return msgMapper.selectByPrimaryKey(id);
    }


    @Override
    public List<MsgCount> getReceiveMsgCountsById(Integer id) {
        return msgAnswerMapper.selectCountByMsgId(id);
    }

    @Override
    public List<MsgCount> getAllCountsByUserId(Integer userId) {
        return msgMapper.selectAllCountByUserId(userId);
    }

    @Override
    public List<MsgCount> getDoneCountsByUserId(Integer userId) {
        return msgMapper.selectDoneCountByUserId(userId);
    }

    @Override
    public List<MsgCount> getAllCountsByAreaId(Integer areaId) {
        return msgMapper.selectAllCountByAreaId(areaId);
    }

    @Override
    public List<MsgCount> getDoneCountsByAreaId(Integer areaId) {
        return msgMapper.selectDoneCountByAreaId(areaId);
    }



}
